package com.example.widget_lib.widget.ring;


import com.example.widget_lib.ResourceTable;
import com.example.widget_lib.bean.CircleBean;
import com.example.widget_lib.bean.RingVBean;
import com.example.widget_lib.utils.AttrUtils;
import com.example.widget_lib.utils.LogUtil;
import com.example.widget_lib.utils.PxUtil;
import com.example.widget_lib.widget.base.CircleCenterStyle;
import com.example.widget_lib.widget.timeruler.TimeRulerPickScaleView;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.ProgressBar;
import ohos.agp.render.Arc;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.PixelMapHolder;
import ohos.agp.utils.*;
import ohos.app.Context;
import ohos.global.resource.Resource;
import ohos.global.resource.solidxml.TypedAttribute;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.multimodalinput.event.TouchEvent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 自定义分段圆环（内含横线和说明绘制）
 *
 * @author hongbing
 * @since 20181030
 * 描述:用于数据模块的统计显示
 * 借鉴：https://github.com/bingoogolapple/BGAProgressBar-Android
 */
public class RingView extends ProgressBar implements Component.TouchEventListener, Component.DrawTask {
    private static final String TAG = RingView.class.getSimpleName();
    public final static int[] COLORS = {
            Color.rgb(88, 181, 250), // 蓝色
            Color.rgb(255, 196, 0), // 黄色
            Color.rgb(101, 226, 175),// 绿色
            Color.rgb(255, 105, 83), //红色
            Color.rgb(202, 129, 222), // 紫色
            Color.rgb(153, 153, 153), // 灰色
            Color.rgb(233, 255, 83) // 浅黄色
    };
    /**
     * 客群颜色专用
     */
    public final static int[] COLORS_KQ = {
            // 男
            Color.rgb(88, 181, 250), // a:255
            Color.rgb(114, 192, 250), // a:215
            Color.rgb(140, 204, 251), // a:175
            Color.rgb(166, 215, 252), // a:135
            Color.rgb(192, 227, 253), // a:95
            Color.rgb(225, 241, 254), // a:45

            // 女
            Color.rgb(255, 105, 83), // a:255
            Color.rgb(255, 129, 110), // a:215
            Color.rgb(255, 152, 137), // a:175
            Color.rgb(255, 175, 164), // a:135
            Color.rgb(255, 199, 191), // a:95
            Color.rgb(255, 228, 224) // a:45
    };
    private static final float DEGREE = 1.f; // 分割线度数

    /**
     * 自定义属性
     */
    private Mode mMode;
    private CenterStyle mCenterStyle; // 图形中间样式
    private boolean mOuterIsEnable; // 边线是否可点击
    private int mReachedColor;
    private int mReachedHeight;
    private int mUnReachedColor;
    private int mUnReachedHeight;
    private int mTextColor;
    private int mTextSize;
    private int mOriginRadius;
    private int mRadius;
    private PixelMap mCenterBitmap;
    private String mCenterTxt;
    private float mCenterTxtSize;
    private int mCenterTxtColor;
    private float mCenterImgTxtMargin;

    private Paint mPaint; // 圆环画笔
    private Paint mLinePaint; // 横线画笔
    private Paint mTxtPaint; // 文本画笔
    private int mMaxStrokeWidth;
    private RectFloat mArcRectF;
    private List<RingVBean> mOuterRfList; // 边线矩阵集合
    private int[] mColors = COLORS;
    //    private List<Integer> mColors = new ArrayList<>();
    private boolean mIsCut = false; // 是否切分割线，默认false
    private String mTopTxt, mBtmTxt;
    //状态栏的高度：base_slice.xml里面的加起来->10 + 10 + 5 + 5 + 15
    private float statusBarHeight = PxUtil.vp2px(45);

    public RingView(Context context) {
        this(context, null);
    }

    public RingView(Context context, AttrSet attrs) {
        this(context, attrs, "");
    }

    public RingView(Context context, AttrSet attrs, String defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initDefaultAttrs();
        initCustomAttrs(attrs);
        setTouchEventListener(this);
//        setEstimateSizeListener(this::onEstimateSize);
        addDrawTask(this);
        mOuterRfList = new ArrayList<>(7);
        mMaxStrokeWidth = Math.max(mReachedHeight, mUnReachedHeight);
    }


    private void initDefaultAttrs() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);

        mTxtPaint = new Paint();
        mTxtPaint.setAntiAlias(true);
        mTxtPaint.setStyle(Paint.Style.FILL_STYLE);

        mLinePaint = new Paint();
        mLinePaint.setAntiAlias(true);
        mLinePaint.setStyle(Paint.Style.FILL_STYLE);
        mLinePaint.setStrokeWidth(3);

        mMode = Mode.Circle;
        mTextColor = Color.getIntColor("#70A800");
        mTextSize = (int) PxUtil.fp2px(10);
        mReachedColor = Color.getIntColor("#FF4081");
        mReachedHeight = (int) PxUtil.vp2px(5);
        mUnReachedColor = Color.getIntColor("#3F51B5");
        mUnReachedHeight = (int) PxUtil.vp2px(5);

        mOriginRadius = (int) PxUtil.vp2px(40);

    }

    private void initCustomAttrs(AttrSet attrs) {
        setBindStateChangedListener(new BindStateChangedListener() {
            @Override
            public void onComponentBoundToWindow(Component component) {
                if (mMode == Mode.Circle || mMode == Mode.MORE || mMode == Mode.GROUP) {
                    int expectSize = mOriginRadius * 2 + mMaxStrokeWidth + getPaddingLeft() + getPaddingRight();

                    mRadius = (expectSize - getPaddingTop() - getPaddingBottom() - mMaxStrokeWidth) / 2;
                    if (mArcRectF == null) {
                        mArcRectF = new RectFloat();
                    }
                    mArcRectF.fuse(0, 0, mRadius * 2, mRadius * 2);

                }
            }

            @Override
            public void onComponentUnboundFromWindow(Component component) {

            }
        });

        mTextColor = AttrUtils.getColorFromAttr(attrs, "ringView_rv_textColor", mTextColor);
        mTextSize = AttrUtils.getDimensionFromAttr(attrs, "ringView_rv_textSize", mTextSize);
        mReachedColor = AttrUtils.getColorFromAttr(attrs, "ringView_rv_rColor", mReachedColor);
        mReachedHeight = AttrUtils.getDimensionFromAttr(attrs, "ringView_rv_rHeight", mReachedHeight);
        mUnReachedColor = AttrUtils.getColorFromAttr(attrs, "ringView_rv_unReachedColor", mUnReachedColor);
        mOriginRadius = AttrUtils.getDimensionFromAttr(attrs, "ringView_rv_radius", mOriginRadius);
        mRadius = mOriginRadius;
        mCenterTxt = AttrUtils.getStringFromAttr(attrs, "ringView_rv_center_txt", "测试");
        mCenterTxtSize = AttrUtils.getDimensionFromAttr(attrs, "ringView_rv_center_txt_size", (int) PxUtil.fp2px(12));
        mCenterTxtColor = AttrUtils.getColorFromAttr(attrs, "ringView_rv_center_txt_color", Color.getIntColor("#2A2A2A"));
        mCenterImgTxtMargin = AttrUtils.getDimensionFromAttr(attrs, "ringView_rv_center_imgtxt_margin", 0);
        mIsCut = AttrUtils.getBooleanFromAttr(attrs, "ringView_rv_need_cut", false);
        mOuterIsEnable = AttrUtils.getBooleanFromAttr(attrs, "ringView_rv_outer_enable", false);
        mMode = Mode.values()[AttrUtils.getIntegerFromAttr(attrs, "ringView_rv_mode", 0)];
        mCenterStyle = CenterStyle.values()[AttrUtils.getIntegerFromAttr(attrs, "ringView_rv_center_style", 0)];

        try {
            Resource resource = getResourceManager().getResource(ResourceTable.Media_widget_icon_person);
            mCenterBitmap = ImageSource.create(resource, new ImageSource.SourceOptions()).createPixelmap(null);
        } catch (Exception e) {
            LogUtil.loge("++++", "出错了");
            e.printStackTrace();
        }

    }

    @Override
    public void addDrawTask(DrawTask task) {
        super.addDrawTask(task);
        task.onDraw(this, mCanvasForTaskOverContent);
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        if (mMode == Mode.Circle) {
            onDrawCircleByTwo(canvas);
        } else {
            if (getVisibility()==Component.HIDE){
                return;
            }
            onDrawCircleByMore(canvas);
        }
//        drawRectToUseDebug(canvas);
    }

    private float downX = 0f, downY = 0f;
    private long currentMS = 0L;

    /**
     * 设置中间图片
     * @param imagePicId
     */
    public void setImagePic(int imagePicId) {
        try {
            Resource resource = getResourceManager().getResource(imagePicId);
            mCenterBitmap = ImageSource.create(resource, new ImageSource.SourceOptions()).createPixelmap(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 绘制圆形的背景和中心图标
     *
     * @param canvas
     */
    private void drawCircleBg(Canvas canvas) {
        mPaint.setStyle(Paint.Style.STROKE_STYLE);
        mPaint.setColor(new Color(mUnReachedColor));
        mPaint.setStrokeWidth(mUnReachedHeight);
        canvas.drawCircle(mRadius, mRadius, mRadius, mPaint);
    }

    /**
     * 根据圆心、半径、角度获取圆上的任意点
     *
     * @param angle 角度
     * @return 任意点坐标
     * 数学公式：https://www.shuxuele.com/sine-cosine-tangent.html
     */
    private float[] getPointByAngle(float angle) {
        float x, y;
        x = (float) (mRadius + mRadius * Math.cos(angle * Math.PI / 180));
        y = (float) (mRadius + mRadius * Math.sin(angle * Math.PI / 180));
        return new float[]{x, y};
    }

    private String[] mStrs;

    /**
     * setData
     * @param progress  进度
     * @param imageType    图形中间样式
     * @param strs  显示的描述
     */
    public void setData(float progress, int imageType, String... strs) {
        if (mOuterRfList != null && mOuterRfList.size() > 0) {
            mOuterRfList.clear();
        }
        if (mStrs != null && mStrs.length > 0) {
            mStrs = null;
        }
        mMode = Mode.Circle;
        mStrs = strs;
        mCenterStyle = CenterStyle.values()[imageType];
        // 将百分制转换成360度
        progress = getMax() * progress / 100;
//        setProgress((int) progress);
        setProgressValue((int) progress);
        setProgressColor(new Color(Color.getIntColor("#00000000")));
        invalidate();
    }

    /**
     * setData
     * @param progress  进度
     * @param strs  显示的描述
     */
    public void setData(float progress, String... strs) {
        if (mOuterRfList != null && mOuterRfList.size() > 0) {
            mOuterRfList.clear();
        }
        if (mStrs != null && mStrs.length > 0) {
            mStrs = null;
        }
        mMode = Mode.Circle;
        mStrs = strs;
        // 将百分制转换成360度
        progress = getMax() * progress / 100;
//        setProgress((int) progress);
        setProgressValue((int) progress);
        setProgressColor(new Color(Color.getIntColor("#00000000")));
        invalidate();
    }

    private List<CircleBean> mList = new ArrayList<>();

    /**
     * setData
     * @param beans 数据
     * @param type   进度条（圆弧）类型
     * @param imageType   图形中间样式
     */
    public void setData(List<CircleBean> beans, int type, int imageType) {
        LogUtil.loge("======", "beans.size()" + "=====");
        if (mOuterRfList != null && mOuterRfList.size() > 0) {
            mOuterRfList.clear();
        }
        if (mList != null && mList.size() > 0) {
            mList.clear();
        }
        mCenterStyle = CenterStyle.values()[imageType];
        mMode = Mode.values()[type];
        mList = beans;
        invalidate();
    }

    /**
     * setData
     * @param beans
     */
    public void setData(List<CircleBean> beans) {
        LogUtil.loge("======", "beans.size()" + "=====");
        if (mOuterRfList != null && mOuterRfList.size() > 0) {
            mOuterRfList.clear();
        }
        if (mList != null && mList.size() > 0) {
            mList.clear();
        }
        mList = beans;
        invalidate();
    }

    /**
     * 绘制圆环中心视图
     *
     * @param canvas
     * @throws IllegalArgumentException 图形结合模式下rv_center_txt需要配置
     */
    private void drawCenterView(Canvas canvas) {
        LogUtil.loge("mCenterStyle", "++" + mCenterStyle);
        PixelMapHolder holder = new PixelMapHolder(mCenterBitmap);
        if (mCenterStyle == CenterStyle.Icon) { // 绘制中间图标
            canvas.drawPixelMapHolder(holder, mRadius - mCenterBitmap.getImageInfo().size.width / 2, mRadius - mCenterBitmap.getImageInfo().size.height / 2, mPaint);
            LogUtil.loge("+++++", "+++++1" + mCenterStyle);
        } else if (mCenterStyle == CenterStyle.Txt) { // 绘制中间文字
            Paint paint = new Paint();
            paint.setAntiAlias(true);
            paint.setDither(true);
            paint.setTextSize((int) mCenterTxtSize);
            paint.setColor(new Color(mCenterTxtColor));
            int[] wh = getTextWH(mCenterTxt, paint);
            canvas.drawText(paint, mCenterTxt, mRadius - wh[0] / 2, mRadius + wh[1] / 2); // 字体开始绘制位置y的值是从字体底部开始算的
        } else if (mCenterStyle == CenterStyle.DOUBLE_TXT) { // 两行文字
            if ((mTopTxt != null && mTopTxt.length() > 0) && (mBtmTxt != null && mBtmTxt.length() > 0)) {
                Paint paint = new Paint();
                paint.setAntiAlias(true);
                paint.setDither(true);
                paint.setTextSize((int) mCenterTxtSize);
                paint.setColor(new Color(mCenterTxtColor));

                int[] whTop = getTextWH(mTopTxt, paint);
                int[] whBtm = getTextWH(mBtmTxt, paint);

                float halfTxtTxtH = (whTop[1] + mCenterImgTxtMargin + whBtm[1]) / 2;
                float bmpTop = mRadius - halfTxtTxtH + whTop[1]; // 需要加上第一个字的高度

                canvas.drawText(paint, mTopTxt, mRadius - whTop[0] / 2, bmpTop);
                canvas.drawText(paint, mBtmTxt, mRadius - whBtm[0] / 2, bmpTop + mCenterImgTxtMargin + whBtm[1]);
            }
        } else {// 文字 + 图标组合
            if (mCenterBitmap == null) {
                throw new IllegalArgumentException("图形结合模式下rv_center_bmp需要配置");
            }
            LogUtil.loge(mCenterTxt.length() + "size");
            if (TextTool.isNullOrEmpty(mCenterTxt)) {
                throw new IllegalArgumentException("图形结合模式下rv_center_txt需要配置");
            }
            Paint paint = new Paint();
            paint.setAntiAlias(true);
            paint.setDither(true);
            paint.setTextSize((int) mCenterTxtSize);
            paint.setColor(new Color(mCenterTxtColor));
            int[] wh = getTextWH(mCenterTxt, paint);

            float halfBmpTxtH = (mCenterBitmap.getImageInfo().size.height + mCenterImgTxtMargin + wh[1]) / 2;
            float bmpTop = mRadius - halfBmpTxtH;

            LogUtil.loge("+++++", "+++++2");
            canvas.drawPixelMapHolder(holder, mRadius - mCenterBitmap.getImageInfo().size.width / 2, bmpTop, mPaint);
            canvas.drawText(paint, mCenterTxt, mRadius - wh[0] / 2, bmpTop + mCenterBitmap.getImageInfo().size.height + mCenterImgTxtMargin + wh[1]); // 字体开始绘制位置y的值是从字体底部开始算的
        }
    }

    /**
     * 绘制两段圆弧
     *
     * @param canvas
     */
    private void onDrawCircleByTwo(Canvas canvas) {
        if (mStrs == null || mStrs.length < 1) {
            return;
        }
        // 1.移动画布保证居中对齐
        canvas.save();
        canvas.translate(getWidth() / 2 - mRadius - mMaxStrokeWidth / 2, getPaddingTop() + mMaxStrokeWidth / 2);

        // 2.绘制图形背景圆和中间图
        drawCircleBg(canvas);
        drawCenterView(canvas);

        // 3.绘制进度值和边线等操作
        mPaint.setStyle(Paint.Style.STROKE_STYLE);
        mPaint.setColor(new Color(mReachedColor));
        mPaint.setStrokeWidth(mReachedHeight);
        float sweepAngle = (getProgress() * 1.0f / getMax()) * 360;
        LogUtil.loge("============", sweepAngle + "#======");
        canvas.drawArc(mArcRectF, new Arc(0f, sweepAngle, false), mPaint);
        int width = getWidth();
        // 绘制两条腿和对应的文案
        mPaint.setStyle(Paint.Style.FILL_STYLE);
        mPaint.setStrokeWidth(3);
        float[] xy; // 圆弧中心点
        int[] wh;
        String currTxt;
        mTxtPaint.setStyle(Paint.Style.FILL_STYLE);
        mTxtPaint.setColor(new Color(mTextColor));
        mTxtPaint.setTextSize(mTextSize);
        for (int i = 0; i < 2; i++) {
            switch (i) {
                case 0:
                    xy = getPointByAngle(sweepAngle / 2);
                    mPaint.setColor(new Color(mReachedColor)); // 黄色
                    break;
                default:
//                    xy = getPointByAngle(sweepAngle + (getMax() - sweepAngle) / 2);
                    xy = getPointByAngle(sweepAngle + (getMax() - sweepAngle / 360) / 2);
                    mPaint.setColor(new Color(mUnReachedColor)); // 蓝色
                    break;
            }

            currTxt = mStrs[i];
            wh = getTextWH(currTxt, mTxtPaint);

            // 判断横线是画在左边还是右边
            float endX;
            boolean isLeft = false;
            if (xy[0] >= mRadius) { // 右边
                endX = xy[0] + mRadius / 2;
            } else { // 左边
                endX = xy[0] - mRadius / 2;
                isLeft = true;
            }
            // 画腿咯
            Point paintStart = new Point(xy[0], xy[1]);
            Point paintEnd = new Point(endX, xy[1]);
            canvas.drawLine(paintStart, paintEnd, mPaint);
            // 画文案咯
            if (currTxt != null && currTxt.length() > 0) {
                LogUtil.loge("201811191753", "currTxt = " + currTxt);
                if (isLeft) {
                    float l_startX = endX - wh[0] - PxUtil.vp2px(3);
                    float l_startY = xy[1] + wh[1] / 2;
                    canvas.drawText(mTxtPaint, currTxt, l_startX, l_startY);

                    // 图形起始点x
                    float cir_x_yd_l = width / 2 - mRadius;
                    addRingVBean(currTxt, cir_x_yd_l + l_startX, l_startY + getPaddingTop(), wh);
                } else {
                    float r_startX = endX + PxUtil.vp2px(3);
                    float r_startY = xy[1] + wh[1] / 2;
                    canvas.drawText(mTxtPaint, currTxt, r_startX, r_startY);

                    // 将图表坐标转换成屏幕坐标
                    float cir_x_yd_r = width / 2 + mRadius;
                    float result_x;
                    if (r_startX < mRadius * 2) { // 圆内
                        result_x = cir_x_yd_r - (mRadius * 2 - r_startX);
                    } else if (r_startX > mRadius * 2) { // 圆外
                        result_x = cir_x_yd_r + r_startX - mRadius * 2;
                    } else { // 圆心
                        result_x = cir_x_yd_r;
                    }
                    addRingVBean(currTxt, result_x, r_startY + getPaddingTop(), wh);
                }
            }
        }
        canvas.restore();
    }

    private void onDrawCircleByMore(Canvas canvas) {
        if (mList == null || mList.size() < 1) {
            LogUtil.loge("==========ssssss");
            return;
        }
        LogUtil.loge("=====", "====onDrawCircleByMore=");
        // 1.移动画布保证居中对齐
        canvas.save();
        canvas.translate(getWidth() / 2 - mRadius - mMaxStrokeWidth / 2, getPaddingTop() + mMaxStrokeWidth / 2);

        // 2.绘制中间图标
        drawCenterView(canvas);

        // 3.相关画笔初始化
        mPaint.setStyle(Paint.Style.STROKE_STYLE);
        mPaint.setStrokeWidth(mReachedHeight);

        mTxtPaint.setColor(new Color(mTextColor));
        mTxtPaint.setTextSize(mTextSize);

        float[] xy;
        int[] wh;
        String currTxt;


        if (mMode == Mode.GROUP) { // 客群标配颜色值
            mColors = COLORS_KQ;
        }

        // 4.逐一绘制圆环、横线、文案
        int size = mList.size();
        if (size > mColors.length) {
            size = mColors.length;
        }
        for (int i = 0; i < size; i++) {
            CircleBean bean = mList.get(i);
            mPaint.setColor(new Color(mColors[i]));
            if (mIsCut) {
                if (bean.getEndPro() > DEGREE) {
                    canvas.drawArc(mArcRectF, new Arc(bean.getStartPro() * 360 / 100, (bean.getEndPro() - DEGREE) * 360 / 100, false), mPaint);
                } else {
                    if (bean.getEndPro() > 0f) { // 注意：当前对象没有进度值，不做绘制处理
                        canvas.drawArc(mArcRectF, new Arc(bean.getStartPro() * 360 / 100, bean.getEndPro() * 360 / 100, false), mPaint);
                    }
                }
            } else {
                if (bean.getEndPro() > 0f) { // 注意：当前对象没有进度值，不做绘制处理
                    canvas.drawArc(mArcRectF, new Arc(bean.getStartPro() * 360 / 100, bean.getEndPro() * 360 / 100, false), mPaint);
                }
            }

            currTxt = bean.getDesc();
            if (currTxt == null || currTxt.length() < 1) { // 没配置描述就不绘制UI
                continue;
            }

            mLinePaint.setColor(new Color(mColors[i]));
            xy = getPointByAngle(bean.getCenterPro() * 360 / 100);
            wh = getTextWH(bean.getDesc(), mTxtPaint);


            LogUtil.loge("====", mRadius + "====中心点 = " + bean.getCenterPro() + "->对应值 = " + bean.getDesc());

            // 判断横线是画在左边还是右边
            float endX;
            boolean isLeft = false;
            if (xy[0] >= mRadius) { // 右边,毛边长度针对0~45,0~-45(360~315)度角做缩短处理(避免小屏幕显示不全现象)
//                if ((bean.getCenterPro() <= 45 && bean.getCenterPro() > 0)
//                        || (bean.getCenterPro() >= 315 && bean.getCenterPro() < 360)) {
//                    endX = xy[0] + mRadius / 2;  /* 原本是 mRadius / 4*/
//                } else {
//                    endX = xy[0] + mRadius / 2;
//                }
                endX = xy[0] + mRadius / 2;  /* 原本是 mRadius / 4*/
            } else { // 左边,毛边长度针对135~180,180~-225度角做缩短处理(避免小屏幕显示不全现象)
//                if ((bean.getCenterPro() >= 135 && bean.getCenterPro() <= 180)
//                        || (bean.getCenterPro() >= 180 && bean.getCenterPro() <= 225)) {
//                    endX = xy[0] - mRadius / 2; /*原本是 mRadius / 2*/
//                } else {
//                    endX = xy[0] - mRadius / 2;
//                }
                endX = xy[0] - mRadius / 2; /*原本是 mRadius / 2*/
                isLeft = true;
            }
            LogUtil.loge("=====", "=====xxxxx===xy[0]=" + xy[0] + "==== xy[1]=" + xy[1]);
            // 画腿咯
            Point start = new Point(xy[0], xy[1]);
            Point end = new Point(endX, xy[1]);
            canvas.drawLine(start, end, mLinePaint);
            // 画文案咯
            if (isLeft) {
                float l_startX = endX - wh[0] - PxUtil.vp2px(3);
                float l_startY = xy[1] + wh[1] / 2;
                canvas.drawText(mTxtPaint, currTxt, l_startX, l_startY);

                int cir_x_yd_l = getWidth() / 2 - mRadius;
                LogUtil.loge(cir_x_yd_l + ":cir_x_yd_l========l_startX:" + l_startX);

                addRingVBean(currTxt, cir_x_yd_l + l_startX, l_startY + getPaddingTop(), wh);
            } else {
                float r_startX = endX + PxUtil.vp2px(3);
                float r_startY = xy[1] + wh[1] / 2;
                canvas.drawText(mTxtPaint, currTxt, r_startX, r_startY);

                float cir_x_yd_r = getWidth() / 2 + mRadius;
                float result_x;
                if (r_startX < mRadius * 2) {
                    result_x = cir_x_yd_r - (mRadius * 2 - r_startX);
                } else if (r_startX > mRadius * 2) {
                    result_x = cir_x_yd_r + r_startX - mRadius * 2;
                } else {
                    result_x = cir_x_yd_r;
                }
                addRingVBean(currTxt, result_x, r_startY + getPaddingTop(), wh);
            }
        }
        LogUtil.loge("=====", "=====33333");
        canvas.restore();
    }

    private void addRingVBean(String label, float x, float y, int[] wh) {
        // 额外补加的间距值，目的是更容易点击到
        int bj_margin = (int) PxUtil.vp2px(4);
        RingVBean rvb = new RingVBean();
        rvb.setLabel(label);
        rvb.setRf(new RectFloat(x - mPaint.getStrokeWidth() - PxUtil.vp2px(2) - bj_margin,
                y - wh[1] / 2 - bj_margin,
                x + wh[0] + bj_margin,
                y + wh[1] / 2 + bj_margin));

        mOuterRfList.add(rvb);
    }

    @Override
    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
        if (!mOuterIsEnable) {
//            return super.onTouchEvent(event);
            return false;
        }
        switch (touchEvent.getAction()) {
            case TouchEvent.PRIMARY_POINT_DOWN:
                int index = touchEvent.getIndex();
                downX = touchEvent.getPointerPosition(index).getX() - getLeft();
                downY = touchEvent.getPointerPosition(index).getY() - getTop() - statusBarHeight;
                currentMS = System.currentTimeMillis();
                LogUtil.loge("MotionEvent.ACTION_DOWN->currentMS = " + currentMS);
                break;
            case TouchEvent.POINT_MOVE:
                LogUtil.loge("MotionEvent.ACTION_MOVE");
                break;
            case TouchEvent.PRIMARY_POINT_UP:
                long moveTime = System.currentTimeMillis() - currentMS;
                LogUtil.loge( "moveTime = " + moveTime);
                if (moveTime < 300) { // 点击判定条件
                    if (mOuterRfList != null && mOuterRfList.size() > 0) {
                        RingVBean bean;
                        int size = mOuterRfList.size();
                        for (RingVBean ringVBean : mOuterRfList) {
                            bean = ringVBean;
                            try {
                                LogUtil.loge("矩阵 = " + bean.getRf() + "->downX = " + downX +
                                        "->downY = " + downY + "->是否包含 = " + bean.isInChartArea(bean.getRf(), downX, downY) + "->size = " + size);

                            } catch (Exception e) {
                                LogUtil.loge("=====Exception=====");
                            }

                            if (bean.isInChartArea(bean.getRf(), downX, downY)) {
                                LogUtil.loge("00000000000000000000000000000");
                                if (mListener != null) {
                                    LogUtil.loge("1111111111111111111111111111111");
                                    mListener.onOuterClick(bean.getLabel());
                                }
                                break;
                            }
                        }
                    }
                }
                break;
            default:
                break;
        }
        return true;
    }

    private enum Mode {
        /*圆形进度条 */
        Circle,
        /*圆形进度条-多段*/
        MORE,
        /* 圆形进度条-客群*/
        GROUP
    }

    private enum CenterStyle {
        /**
         * 图片
         */
        Icon,
        /**
         * 文字
         */
        Txt,
        /**
         * 两行文字
         */
        DOUBLE_TXT,
        /**
         * 文字和图片
         */
        All
    }

//    public void setColorsArr(int[] colors) {
//        mColors = colors;
//    }

    public void setColors(int... colors) {
//        mColors.clear();
//        for (int i = 0; i < colors.length; i++) {
//            mColors.add(colors[i]);
//        }
        mColors = colors;
    }

    public int[] getColors() {
        return mColors;
    }

    public void setCenterTxt(String txt) {
        mCenterTxt = txt;
    }

    public void setCenterTxtColor(int txtColor) {
        mCenterTxtColor = txtColor;
    }

    public void setCenterTxtColor(String txt, int txtColor) {
        setCenterTxt(txt);
        mCenterTxtColor = txtColor;
    }

    public void setCenterTxt(String topTxt, String btmTxt) {
        mTopTxt = topTxt;
        mBtmTxt = btmTxt;
    }

    public void setOuterIsEnable(boolean outerIsEnable) {
        mOuterIsEnable = outerIsEnable;
    }

    public void setCenterTxtColor(String topTxt, String btmTxt, int txtColor) {
        setCenterTxt(topTxt, btmTxt);
        mCenterTxtColor = txtColor;
    }

    /**
     * 获取文字的宽度和高度
     *
     * @param text  绘制的文字
     * @param paint 画笔
     * @return 文字的宽高
     */
    public int[] getTextWH(String text, Paint paint) {
        if (text == null || text.length() < 1) {
            return new int[]{0, 0};
        }
//        RectFloat bounds = new RectFloat();
        Rect textBounds = paint.getTextBounds(text);
        int width = textBounds.left + textBounds.getWidth();
        int height = textBounds.bottom + textBounds.getHeight();
        return new int[]{width, height};
    }

    private OuterListener mListener;

    public void setListener(OuterListener listener) {
        mListener = listener;
    }

    public interface OuterListener {
        void onOuterClick(String label);
    }
}
